<?php
/**
 * @author GhostWei
 * @date Aug 20, 2012
 */
class picture_special_regroup_concept extends system_concept_abstract {
	
	public function __construct($lang) {
		parent::__construct(array('lang' => $lang), array('lang = ?' => $lang));
	}
	
	public function init() {
		$this->setModel('picture_special_regroup_model', 'regroup');
		$this->setModelCols('regroup', array('id', 'lang', 'name', 'description', 'creationtime'));
	}

	public function save($row) {
		if(!system_setting_language::verify($row->getLang())) {
			throw new picture_exception("Language(SPECIAL-TOPIC) formation error.");
		}
		if(!$row->getName()) {
			throw new picture_exception("Name can not be empty.");
		}
		if(!$row->getDescription()) {
			throw new picture_exception("Description can not be empty.");
		}
		
		if($row->getId()) {
			if($this->fetch(array('id != ?' => $row->getId(), 'name = ?' => $row->getName()))) {
				throw new picture_exception("This Name({$row->getName()}) is already Existing.");
			}
			$row->update();
		} else {
			if($this->fetch(array('name = ?' => $row->getName()))) {
				throw new picture_exception("This Name({$row->getName()}) is already Existing.");
			}
			$row->setCreationtime(system_date::timestamp());
			$id = $this->getModel('regroup')->insert($row->getModelData('regroup'));
			$row->setId($id);
		}
		return $row;
	}
	
	public function setName($row, $name) {
		if(!$name) {
			$row->set('name', '');
		} else {
			$name = preg_replace('/[^a-z0-9]+/is', '', $name);
			$row->set('name', $name);
		}
	}
	
	public function delete($row) {
		$model = new picture_special_recommend_model();
		$model->delete("group_id = {$row->getId()}");
		return parent::delete($row);
	}

}
